package com.liwenxu;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

import com.tutk.IOTC.AVAPIs;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Created by 李文旭 on 2018/9/9.
 */

public class AudioThread implements Runnable {
    static final int AUDIO_BUF_SIZE = 1024;
    static final int FRAME_INFO_SIZE = 16;
    public  AudioTrack track;
    private int avIndex;
    public static int stop=0;
    public AudioThread(int avIndex) {
        this.avIndex = avIndex;

    }

    @Override
    public void run() {
        System.out.printf("[%s] Start\n",
                Thread.currentThread().getName());

        AVAPIs av = new AVAPIs();
        byte[] frameInfo = new byte[FRAME_INFO_SIZE];
        byte[] audioBuffer = new byte[AUDIO_BUF_SIZE];
        while (stop==0) {
            int ret = av.avCheckAudioBuf(avIndex);

            if (ret < 0) {
                // Same error codes as below
                System.out.printf("[%s] avCheckAudioBuf() failed: %d\n",
                        Thread.currentThread().getName(), ret);
                break;
            }
            else if (ret < 3) {
                try {
                    Thread.sleep(120);
                    continue;
                }
                catch (InterruptedException e) {
                    System.out.println(e.getMessage());
                    break;
                }
            }

            int[] frameNumber = new int[1];
            ret = av.avRecvAudioData(avIndex, audioBuffer,
                    AUDIO_BUF_SIZE, frameInfo, FRAME_INFO_SIZE,
                    frameNumber);
            if (ret == AVAPIs.AV_ER_SESSION_CLOSE_BY_REMOTE) {
                System.out.printf("[%s] AV_ER_SESSION_CLOSE_BY_REMOTE\n",
                        Thread.currentThread().getName());
                break;
            }
            else if (ret == AVAPIs.AV_ER_REMOTE_TIMEOUT_DISCONNECT) {
                System.out.printf("[%s] AV_ER_REMOTE_TIMEOUT_DISCONNECT\n",
                        Thread.currentThread().getName());
                break;
            }
            else if (ret == AVAPIs.AV_ER_INVALID_SID) {
                System.out.printf("[%s] Session cant be used anymore\n",
                        Thread.currentThread().getName());
                break;
            }
            else if (ret == AVAPIs.AV_ER_LOSED_THIS_FRAME) {
                //System.out.printf("[%s] Audio frame losed\n",
                //        Thread.currentThread().getName());
                continue;
            }
            else if(ret<0)
            {
                continue;
            }
            short pcm[]=new short[ret];
            G711Code.G711aDecoder(pcm,audioBuffer,ret);
            byte[] newBuffer=new byte[ret*2];
            ByteBuffer.wrap(newBuffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(pcm);
            VideoPlay.AudioQueue.add(new framInfo(newBuffer,ret*2,frameNumber[0]));

        }

        System.out.printf("[%s] Exit\n",
                Thread.currentThread().getName());
    }
}
